//
//  NSString+AttributedString.m
//  XiuXiuTuanGou
//
//  Created by 王克博 on 2018/8/24.
//  Copyright © 2018年 XiuXiuTuanGou. All rights reserved.
//

#import "NSString+AttributedString.h"
#import <UIKit/UIKit.h>

@implementation NSString (AttributedString)

- (NSMutableAttributedString *)getAttributedStringWith:(NSDictionary *)attributesDict range:(NSRange)range
{
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributedString setAttributes:attributesDict range:range];
    return attributedString;
}

- (NSMutableAttributedString *)getParagraphAttributedStringWithLineSpacing:(NSInteger)lineSpacing
{
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    NSMutableParagraphStyle * paragtaphStyle = [NSMutableParagraphStyle new];
    paragtaphStyle.lineSpacing = lineSpacing;
    NSDictionary * dict = @{NSParagraphStyleAttributeName : paragtaphStyle};
    [attributedString setAttributes:dict range:NSMakeRange(0, self.length)];
    return attributedString;
}

- (NSMutableAttributedString *)getTextColorAttributedWithColor:(UIColor *)attColor range:(NSRange)range
{
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributedString addAttribute:NSForegroundColorAttributeName value:attColor range:range];
    return attributedString;
}

- (NSMutableAttributedString *)createAttributingByIcon:(NSString *)icon
                                        downIndexImage:(NSString *)downIndexImage
                                             textColor:(UIColor *)textColor
                                                  font:(UIFont *)font{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    //2.1修改富文本中的不同文字的样式
    [attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, self.length)];//字体颜色
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.length)];//字体大小
    
    
    if (icon &&icon.length >0) {
        
        CGSize size = [UIImage imageNamed:icon].size;
        //3.初始化NSTextAttachment对象
        NSTextAttachment *attchment = [[NSTextAttachment alloc]init];
        attchment.bounds = CGRectMake(0, -3, size.width+1.5, size.height +1.5);//设置frame
        attchment.image = [UIImage imageNamed:icon];//设置图片
        
        //4.创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];
        [attributedString insertAttributedString:string atIndex:0];//插入到第几个下标
    }
    
    if (downIndexImage && downIndexImage.length >0) {
        
        CGSize size = [UIImage imageNamed:downIndexImage].size;
        
        NSTextAttachment *attchment1 = [[NSTextAttachment alloc]init];
        attchment1.bounds = CGRectMake(0, -3, size.width +1.5, size.height +1.5);//设置frame
        attchment1.image = [UIImage imageNamed:downIndexImage];//设置图片
        NSAttributedString *string1 = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment1)];
        
        [attributedString appendAttributedString:string1];
    }
    
    return attributedString;
}

- (NSMutableAttributedString *)createAttributingByIcon:(NSString *)icon
                                        downIndexImage:(NSString *)downIndexImage
                                             textColor:(UIColor *)textColor
                                                  font:(UIFont *)font
                                             imageText:(NSString *)text
                                        imageTextColor:(UIColor *)imageTextColor
                                         imageTextFont:(UIFont *)imageTextFont{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    //2.1修改富文本中的不同文字的样式
    [attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, self.length)];//字体颜色
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.length)];//字体大小
    
    
    if (icon) {
        
        CGSize size = [UIImage imageNamed:icon].size;
        //3.初始化NSTextAttachment对象
        NSTextAttachment *attchment = [[NSTextAttachment alloc]init];
        attchment.bounds = CGRectMake(0, -3, size.width, size.height);//设置frame
        attchment.image = [UIImage imageNamed:icon];//设置图片
        
        //4.创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];
        [attributedString insertAttributedString:string atIndex:0];//插入到第几个下标
    }
    
    if (downIndexImage) {
        
        CGSize size = [UIImage imageNamed:downIndexImage].size;
        UIGraphicsBeginImageContext(size);
        UIImage *image = [UIImage imageNamed:downIndexImage];
        [image drawInRect:CGRectMake(0,0,size.width, size.height)];
        CGRect rect = CGRectMake(8, 1, size.width, size.height);
        [text drawInRect:CGRectIntegral(rect) withAttributes:@{NSFontAttributeName:imageTextFont, NSForegroundColorAttributeName: imageTextColor}];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        NSTextAttachment *attchment1 = [[NSTextAttachment alloc]init];
        attchment1.bounds = CGRectMake(0, -3, size.width, size.height);//设置frame
        attchment1.image = newImage;//设置图片
        NSAttributedString *string1 = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment1)];
        
        [attributedString appendAttributedString:string1];
    }
    
    return attributedString;

}



- (NSMutableAttributedString *)createDeleteStringByTextColor:(UIColor *)textColor font:(UIFont *)font{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    //2.1修改富文本中的不同文字的样式
    [attributedString addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, self.length)];//字体颜色
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.length)];//字体大小
    [attributedString addAttribute:NSStrikethroughStyleAttributeName value:
     [NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, self.length)]; // 删除线类型
    [attributedString addAttribute:NSStrikethroughColorAttributeName value:textColor range:NSMakeRange(0, self.length)]; // 删除线颜色
    
    return attributedString;
}

@end
